/**
 * Created by zhouli on 2019/2/21
 * 路由跳转逻辑统一封装
 */
import { message} from 'antd';
import mileageExpenseService from 'containers/mileage-subsidy-expense/mileage-subsidy-expense.service'
import subApplicationViewService from 'containers/sub-application/sub-application-view.service'
import configureStore from 'stores';
import { setCurrentPageRoute, setRouteIsShow } from 'actions/main';
import menuRoute from "routes/menuRoute";

export default {
  menuRouterDeal: function (item, context, authToken) {
    console.log("激活菜单", item)
    configureStore.store.dispatch(setCurrentPageRoute(item));
    if (typeof item === 'string') {
      //兼容以前的逻辑
      context.router.push(item);
    } else {
      let url = item.thirdTypeUrl || item.url;
      if (typeof url === 'string') {
        context.router.push(url);
      } else {
        /*第三方重定向处理*/
        let {type} = url && url.state;
        if (type === 'credential-export') {
          window.location.href = "http://api.huilianyi.com/jclegend/#/expense-reports";
        } else if (type === 'posting-date') {
          window.location.href = 'http://api.huilianyi.com/jinkoPassedFee/index.html#/passedfee?token=' + authToken.access_token;
        }
      }
    }
  },
  //根据接口获取菜单是否应该显示
  getRouteIsShow: function () {
    let routeIsShow = {};
    return new Promise((resolve, reject) => {
      Promise.all([
        mileageExpenseService.isLadder(),
        subApplicationViewService.getSubApplicationList({manager: false})
      ]).then(res => {
        routeIsShow['mileage-subsidy-expense'] = res[0].data.mode === 9002;
        routeIsShow['sub-application-view'] = res[1].data && res[1].data.length;
        configureStore.store.dispatch(setRouteIsShow(routeIsShow));
        resolve(routeIsShow);
      });
    })
  },
  //前端激活菜单的key，找到后端对应菜单的 resourceId
  getResourceIdByMenu() {
    let currentPage = configureStore.store.getState().main.currentPage;
    let user = configureStore.store.getState().login.user;
    let selectedMenu = menuRoute.getMenuItemByAttr(currentPage[currentPage.length - 1].key, 'key');
    let menu = configureStore.store.getState().login.userPermissionAllMenus;
    let id = false;
    let key = selectedMenu.key;
    if (user.enableNewPermission) {
      let menuList = menu || [];
      menuList && menuList.length > 0 && menuList.map(item => {
        if (item.key === key) {
          id = item.resourceId;
        }
      });
      if (id) {
        return id;
      } else {
        return this.getStaticResourceIdByMenu(key);
      }
    } else {
      return this.getStaticResourceIdByMenu(key);
    }
  },

  //针对老的租户，找静态的 resourceId
  getStaticResourceIdByMenu(key){
    let resoureKeyList = configureStore.store.getState().setting.resourceKey;//后端返回的 key -- resoureId
    if(resoureKeyList && resoureKeyList.length > 0){
      let len = resoureKeyList.length;
      for(let i = 0; i < len; i++){
        let item = resoureKeyList[i];
        if(item.key === key){
          return item.resource_id
        }
      }
    }else {
      return false;
    }
  },
  //对应新的权限系统，找到配置的第一个菜单，进行显示
  getFirstMenu (menu) {
    if(menu.length > 0){
      for(let i = 0,len = menu.length; i < len; i++){
        if(menu[i].url){
          return menu[i];
        }
        if(menu[i].subMenu && menu[i].subMenu.length > 0){
          return this.getFirstMenu(menu[i].subMenu);
        }
      }
    }else {
      message.warning("___没有配置菜单___");
      return null;
    }
  }
}
